package com.example.spark.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spark.dao.BottomDao;
import com.example.spark.entity.Bottom;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.BottomService;
import com.example.spark.service.minio.MinioCloudStorageService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author gky
 * @date 2023/08/26 15:06
 * @description
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BottomServiceImpl extends ServiceImpl<BottomDao, Bottom> implements BottomService {
    @Resource
    private MinioCloudStorageService minIoService;
    @Autowired
    private BottomDao bottomDao;

    @Override
    public CommonResult<Bottom> getBottom() {
        Bottom bottom = bottomDao.selectOne(null);
        return new CommonResult<>(200, "success", bottom);
    }

    @Override
    public CommonResult<String> updateBottom(Bottom bottom) {

        Bottom bottom1 = bottomDao.selectOne(null);
        if (bottom.getId() == null && bottom1 == null) {
            if (bottomDao.insert(bottom) > 0) {
                return new CommonResult<>(200, "success", "添加成功");
            } else {
                return new CommonResult<>(200, "error", "添加失败");
            }
        }else if(bottom.getId()!=null){
            if (bottomDao.updateById(bottom) > 0) {
                return new CommonResult<>(200, "success", "修改成功");
            } else {
                return new CommonResult<>(200, "error", "修改失败");
            }
        }
        return new CommonResult<>(200, "error", "操作失败");
    }

    @Override
    public CommonResult<String> updateImg(String photoName,MultipartFile photo) {
        Bottom bottom = bottomDao.selectOne(null);
        if(bottom==null){
            return new CommonResult<>(200,"error","底部信息不存在");
        }
        // 获取文件扩展名
        String photoExtension = FilenameUtils.getExtension(photo.getOriginalFilename());
        // 上传图片并获取图片的存储路径
        String photoUrl;
        try {
            switch (photoName){
                case "logo":
                    photoUrl = minIoService.uploadPhotoSuffix(photo.getBytes(), photoExtension, "小组logo");
                    bottom.setLogo(photoUrl);
                    break;
                case "schoolBadgeImg":
                    photoUrl = minIoService.uploadPhotoSuffix(photo.getBytes(), photoExtension, "校徽图片");
                    bottom.setSchoolBadgeImg(photoUrl);
                    break;
                case "schoolNameImg":
                    photoUrl = minIoService.uploadPhotoSuffix(photo.getBytes(), photoExtension, "校名图片");
                    bottom.setSchoolNameImg(photoUrl);
                    break;
                case "groupNameImg":
                    photoUrl = minIoService.uploadPhotoSuffix(photo.getBytes(), photoExtension, "组名图片");
                    bottom.setGroupNameImg(photoUrl);
                    break;
                default:
                    return new CommonResult<>(200, "error", "路径名称不合法！");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (bottomDao.updateById(bottom) > 0) {
            return new CommonResult<>(200, "success", "修改成功");
        }
        return new CommonResult<>(200, "error", "修改失败");
    }
}
